import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
Vue.use(VueRouter)

const routes: Array<RouteConfig> = [
  { path: "/", redirect: '/login' },
  { path: "/login", component: () => import('../views/login.vue') },
  {
    path: "/index", redirect: "/start", component: () => import("../views/index.vue"), children: [
      { path: "/start", component: () => import("../views/start.vue") },
      { path: "/user", component: () => import("../views/user.vue") },
      { path: "/message", component: () => import("../views/message.vue") },
      { path: "/option", component: () => import("../views/video/opVideo.vue") },
      { path: "/filter", component: () => import("../views/video/fvideo.vue") },
      { path: "/blog", component: () => import("../views/blog/blogOP.vue") },
      { path: "/chat", component: () => import("../views/blog/eChat.vue") },
      { path:"/write", component:()=> import("../views/blog/blogWrite.vue")},
      { path: "/navigation", component: () => import("../views/navigation/navi.vue") },
      { path: "/pdfChart", component: () => import("../views/pdf/pdfChart.vue") },
      { path: "/pdfOption", component: () => import("../views/pdf/pdf.vue") },
      { path: "/music", component:()=>import("../views/music/music.vue")},
      { path:"/addVideo", component:()=>import("../views/video/addVideo.vue")}
    ]
  }
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
    next()
  }
  else {
    let hasToken: boolean = localStorage.getItem("Token") ? true : false

    if (hasToken) {
      return next()
    } else {
      return next('/login')
    }
  }
})

export default router
